<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/6/9
 * Time: 9:03 上午
 */

namespace app\index\controller;
use app\index\model\doCate;
use think\Db;
use think\facade\Request;
use think\Model;

class Group extends Base
{
    public function g_list(){
        $cate = doCate::all()->toArray();
        $cate = getTree($cate);
        for ($i=0;$i<sizeof($cate);$i++){
            $cate[$i]['title'] =  str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----', $cate[$i]['level']).$cate[$i]['title'];
        }

        $this->assign('cate',$cate);
        return $this->fetch();
    }
    // 添加分类
    public function addCate()
    {
        $post = input('post.');
        if (Request::isPost()){
            if (!empty($post['title'])){
                $c = new doCate();
                $finfo = Db::name('cate')->where("id",$post['pid'])->find();
                if($finfo) $post['lv'] = $finfo['lv']+1;
                else $post['lv'] = 1;
                if($post['pid']==68){
                    $post['lv'] = 2;
                }
                if ( $c->save($post)){
                    return json(['code'=>1,'msg'=>'添加成功']);
                }else{
                    return json(['code'=>2,'msg'=>'网络开小差了,请稍后再试']);
                }
            }else{
                return json(['code'=>2,'msg'=>'分类名称不能为空']);
            }
        }else{
            $this->assign('pid',input('pid'));
            return $this->fetch();
        }


    }


    // 禁用分类
    public function disCate()
    {
        $data = input('post.');

        $res = Db::name("cate")->where("id",$data['id'])->delete();
        if ($res){
            return json(['code'=>1]);
        }else{
            return json(['code'=>0]);
        }
    }

    // 编辑分类
    public function editCate()
    {
        $id = input('id');
        if (Request::isPost()){
            $post = input('post.');
            if (doCate::update($post)){
                return json(['code'=>1]);
            }else{
                return json(['code'=>0]);
            }
        }else{
            $data = doCate::get($id);
            $this->assign('data',$data);
            return $this->fetch();
        }

    }
    //导入分类
    public function excelrun(){

        $file = $_FILES['file'];
        if ($file['error'] == 4) $this->error('请选择上传excel文件');
        $file_types = explode ( ".", $file['name'] );
        $excel_type = array('xls','csv','xlsx','xlsxla');
        if (!in_array(strtolower(end($file_types)),$excel_type)){
            $this->error("不是Excel文件，请重新上传");
        }

        include(getcwd()."/../vendor/PHPExcel/PHPExcel.php");

        if (end($file_types) =='xlsx') {
            $objReader = new \PHPExcel_Reader_Excel2007();
            $objPHPExcel = $objReader ->load($file['tmp_name']);
        } else if (end($file_types) =='xls') {
            $objReader = new \PHPExcel_Reader_Excel5();
            $objPHPExcel = $objReader ->load($file['tmp_name']);
        } else if (end($file_types)=='csv') {
            $PHPReader = new \PHPExcel_Reader_CSV();

            //默认输入字符集
            $PHPReader->setInputEncoding('GBK');

            //默认的分隔符
            $PHPReader->setDelimiter(',');

            //载入文件
            $objPHPExcel = $PHPReader->load($file['tmp_name']);
        }

        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数

        Db::startTrans();
        try{
            //开始读取数据
            for($j=3;$j<=$highestRow;$j++)
            {
                $data['name'] = $objPHPExcel->getActiveSheet()->getCell('A'.$j)->getValue();
                $data['rel_name'] = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();
                $data['sex'] = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();
                $data['phone'] = $objPHPExcel->getActiveSheet()->getCell("D".$j)->getValue();
                $city = $objPHPExcel->getActiveSheet()->getCell("E".$j)->getValue();
//                $area = $objPHPExcel->getActiveSheet()->getCell("F".$j)->getValue();
                $wei = $objPHPExcel->getActiveSheet()->getCell("G".$j)->getValue();
                $if_zhishu = $objPHPExcel->getActiveSheet()->getCell("H".$j)->getValue();
                $data['school_type'] = $objPHPExcel->getActiveSheet()->getCell("I".$j)->getValue();
                $data['time'] = time();
                //查看是否为一级分类

                if($city != "天津市"){

//                    Db::rollback();
                    ajaxMsg('0','城市输入错误');
                }
                 $cate = Db::name("cate")->where('title',$data['name'])->find();
                if($cate){
                    ajaxMsg('0','数据重复'); 
                }
                //判断是否为教委
                if($wei==''){
                    $cate_data['pid'] = 4;
                    $cate_data['title'] = $data['name'];
                    $cate_data['lv'] = 2;
                    $cate_data['status'] = 1;
                    $data['lv'] = 2;
                    $data['rid'] = 9;//教委角色id
                    $data['area'] = Db::name('cate')->insertGetId($cate_data);
                    $data['school'] =  0;
                }else{
                    //学校分类
                    $cate_data['pid'] = Db::name("cate")->where('title',$wei)->find()['id'];
                    if(!$cate_data['pid']){
                        ajaxMsg('0','并无该教委,请调整教委在表格中顺序!');
                    }
                    $cate_data['title'] = $data['name'];
                    $cate_data['lv'] = 3;
                    $cate_data['status'] = 1;
                    $data['lv'] = 3;
                    $data['rid'] = 8;//学校角色id
                    $data['school'] = Db::name('cate')->insertGetId($cate_data);
                    $data['area'] =  $cate_data['pid'];
                }
                if($if_zhishu=='是'){
                    $data['if_own'] = 1;
                }else{
                    $data['if_own'] = 0;
                }
                
                $data['password'] = sha1('123456');
//                dump($data);die;
                Db::name("admin")->insert($data);


            }
//            Db::name('xueshe_acardinfo_test')->insertAll($array);
//             提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            dump($e);
        }
        ajaxMsg('1','导入成功');
    }
}